package com.spic.business.registrationform.controller;

import com.spic.business.registrationform.domain.*;
import com.spic.business.registrationform.service.*;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * 一人一档 (南露天煤矿从业人员登记)Controller
 * 
 * @author spic
 * @date 2022-07-20
 */
@Slf4j
@RestController
@RequestMapping("/staff/record")
public class ArchiveStaffRecordController extends BaseController
{
    @Autowired
    private IArchiveStaffRecordService archiveStaffRecordService;
    @Autowired
    private IArchiveStaffWorkExperienceService archiveStaffWorkExperienceService;
    @Autowired
    private IArchiveStaffPositionChangeService archiveStaffPositionChangeService;
    @Autowired
    private IArchiveStaffPromotionRecordService archiveStaffPromotionRecordService;
    @Autowired
    private IArchiveStaffDisciplinaryRecordsService archiveStaffDisciplinaryRecordsService;
    @Autowired
    private IArchiveStaffPostholdersRecordsService archiveStaffPostholdersRecordsService;
    @Autowired
    private IArchiveStaffJobholderRecordsService archiveStaffJobholderRecordsService;

    /**
     * 查询一人一档 (南露天煤矿从业人员登记)列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ArchiveStaffRecord archiveStaffRecord)
    {
        startPage();
        List<ArchiveStaffRecord> list = archiveStaffRecordService.selectArchiveStaffRecordList(archiveStaffRecord);
        return getDataTable(list);
    }

    /**
     * 导出一人一档 (南露天煤矿从业人员登记)列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArchiveStaffRecord archiveStaffRecord) throws IOException {
        List<ArchiveStaffRecord> list = archiveStaffRecordService.selectArchiveStaffRecordList(archiveStaffRecord);
        ExcelUtil<ArchiveStaffRecord> util = new ExcelUtil<ArchiveStaffRecord>(ArchiveStaffRecord.class);
        util.exportExcel(response, list, "一人一档 (南露天煤矿从业人员登记)数据");
    }

    /**
     * 获取一人一档 (南露天煤矿从业人员登记)详细信息
     */
    @GetMapping(value = "/{staffArchiveId}")
    public AjaxResult getInfo(@PathVariable("staffArchiveId") String staffArchiveId)
    {
        StaffRecordDTO recordDTO = new StaffRecordDTO();
        ArchiveStaffRecord archiveStaffRecord = archiveStaffRecordService.selectArchiveStaffRecordByStaffArchiveId(staffArchiveId);
        recordDTO.setStaffRecord(archiveStaffRecord);

        ArchiveStaffWorkExperience experience = new ArchiveStaffWorkExperience();
        experience.setStaffArchiveId(staffArchiveId);
        List<ArchiveStaffWorkExperience> archiveStaffWorkExperiences = archiveStaffWorkExperienceService.selectArchiveStaffWorkExperienceList(experience);
        recordDTO.setStaffWorkExperience(archiveStaffWorkExperiences);

        ArchiveStaffPositionChange archiveStaffPositionChange = new ArchiveStaffPositionChange();
        archiveStaffPositionChange.setStaffArchiveId(staffArchiveId);
        List<ArchiveStaffPositionChange> archiveStaffPositionChanges = archiveStaffPositionChangeService.selectArchiveStaffPositionChangeList(archiveStaffPositionChange);
        recordDTO.setStaffPositionChange(archiveStaffPositionChanges);

        ArchiveStaffPromotionRecord promotionRecord = new ArchiveStaffPromotionRecord();
        promotionRecord.setStaffArchiveId(staffArchiveId);
        List<ArchiveStaffPromotionRecord> archiveStaffPromotionRecords = archiveStaffPromotionRecordService.selectArchiveStaffPromotionRecordList(promotionRecord);
        recordDTO.setStaffPromotionRecord(archiveStaffPromotionRecords);

        ArchiveStaffDisciplinaryRecords staffDisciplinaryRecords = new ArchiveStaffDisciplinaryRecords();
        staffDisciplinaryRecords.setStaffArchiveId(staffArchiveId);
        List<ArchiveStaffDisciplinaryRecords> archiveStaffDisciplinaryRecords = archiveStaffDisciplinaryRecordsService.selectArchiveStaffDisciplinaryRecordsList(staffDisciplinaryRecords);
        recordDTO.setStaffDisciplinaryRecords(archiveStaffDisciplinaryRecords);

        ArchiveStaffPostholdersRecords staffPostholdersRecords = new ArchiveStaffPostholdersRecords();
        staffPostholdersRecords.setStaffArchiveId(staffArchiveId);
        List<ArchiveStaffPostholdersRecords> archiveStaffPostholdersRecords = archiveStaffPostholdersRecordsService.selectArchiveStaffPostholdersRecordsList(staffPostholdersRecords);
        recordDTO.setStaffPostholdersRecords(archiveStaffPostholdersRecords);

        ArchiveStaffJobholderRecords staffJobholderRecords = new ArchiveStaffJobholderRecords();
        staffJobholderRecords.setStaffArchiveId(staffArchiveId);
        List<ArchiveStaffJobholderRecords> archiveStaffJobholderRecords = archiveStaffJobholderRecordsService.selectArchiveStaffJobholderRecordsList(staffJobholderRecords);
        recordDTO.setStaffJobholderRecords(archiveStaffJobholderRecords);

        return AjaxResult.success(recordDTO);
    }

    /**
     * 修改一人一档 (南露天煤矿从业人员登记)
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody StaffRecordDTO staffRecordDTO)
    {
        archiveStaffRecordService.updateArchiveStaffRecord(staffRecordDTO);
        return AjaxResult.success();
    }

    /**
     * 新增一人一档 (南露天煤矿从业人员登记)
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody StaffRecordDTO staffRecordDTO)
    {
        int i = archiveStaffRecordService.insertArchiveStaffRecord(staffRecordDTO.getStaffRecord());
        List<ArchiveStaffWorkExperience> staffWorkExperience = staffRecordDTO.getStaffWorkExperience();
        if(staffWorkExperience != null && staffWorkExperience.size() > 0) {
            for(ArchiveStaffWorkExperience experience : staffWorkExperience) {
                archiveStaffWorkExperienceService.insertArchiveStaffWorkExperience(experience);
            }
        }
        List<ArchiveStaffPositionChange> staffPositionChange = staffRecordDTO.getStaffPositionChange();
        if(staffPositionChange != null && staffPositionChange.size() > 0) {
            for (ArchiveStaffPositionChange positionChange : staffPositionChange) {
                archiveStaffPositionChangeService.insertArchiveStaffPositionChange(positionChange);
            }
        }
        List<ArchiveStaffPromotionRecord> staffPromotionRecord = staffRecordDTO.getStaffPromotionRecord();
        if(staffPromotionRecord != null && staffPromotionRecord.size() > 0) {
            for (ArchiveStaffPromotionRecord promotionRecord : staffPromotionRecord) {
                archiveStaffPromotionRecordService.insertArchiveStaffPromotionRecord(promotionRecord);
            }
        }
        List<ArchiveStaffDisciplinaryRecords> staffDisciplinaryRecords = staffRecordDTO.getStaffDisciplinaryRecords();
        if(staffWorkExperience != null && staffWorkExperience.size() > 0) {
            for(ArchiveStaffDisciplinaryRecords disciplinaryRecords : staffDisciplinaryRecords) {
                archiveStaffDisciplinaryRecordsService.insertArchiveStaffDisciplinaryRecords(disciplinaryRecords);
            }
        }
        List<ArchiveStaffPostholdersRecords> staffPostholdersRecords = staffRecordDTO.getStaffPostholdersRecords();
        if(staffWorkExperience != null && staffWorkExperience.size() > 0) {
            for(ArchiveStaffPostholdersRecords postholdersRecords : staffPostholdersRecords) {
                archiveStaffPostholdersRecordsService.insertArchiveStaffPostholdersRecords(postholdersRecords);
            }
        }
        List<ArchiveStaffJobholderRecords> staffJobholderRecords = staffRecordDTO.getStaffJobholderRecords();
        if(staffWorkExperience != null && staffWorkExperience.size() > 0) {
            for(ArchiveStaffJobholderRecords jobholderRecords : staffJobholderRecords) {
                archiveStaffJobholderRecordsService.insertArchiveStaffJobholderRecords(jobholderRecords);
            }
        }

        return toAjax(i);
    }

    /**
     * excel 批量导入
     * @param file
     * @return
     */
    @PostMapping(value = "/importData")
    public AjaxResult importData(@RequestPart(value = "file", required = true) MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        archiveStaffRecordService.importData(inputStream);
        return AjaxResult.success();
    }

    /**
     * 下载导入模板
     *
     * @return
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        InputStream inputStream = null;
        ServletOutputStream servletOutputStream = null;
        try {
            servletOutputStream = response.getOutputStream();
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("一人一档模板.xls", "utf8"));
            ClassPathResource classPathResource = new ClassPathResource("Excel_model/一人一档模板.xls");
            inputStream = classPathResource.getInputStream();
            IOUtils.copy(inputStream, servletOutputStream);
            response.flushBuffer();
        } catch (IOException e) {
            log.info(e.getMessage());
        } catch (RuntimeException e) {
            log.info(e.getMessage());
        } finally {
            IOUtils.closeQuietly(servletOutputStream);
            IOUtils.closeQuietly(inputStream);
        }

    }

    /**
     * 删除一人一档 (南露天煤矿从业人员登记)
     */
	@DeleteMapping("/{staffArchiveIds}")
    @Transactional
    public AjaxResult remove(@PathVariable String[] staffArchiveIds)
    {
        archiveStaffRecordService.deleteArchiveStaffRecordByStaffArchiveIds(staffArchiveIds);
        archiveStaffWorkExperienceService.deleteArchiveStaffWorkExperienceByStaffArchiveIds(staffArchiveIds);
        archiveStaffPositionChangeService.deleteArchiveStaffPositionChangeByStaffArchiveIds(staffArchiveIds);
        archiveStaffPromotionRecordService.deleteArchiveStaffPromotionRecordByStaffArchiveIds(staffArchiveIds);
        archiveStaffDisciplinaryRecordsService.deleteArchiveStaffDisciplinaryRecordsByStaffArchiveIds(staffArchiveIds);
        archiveStaffPostholdersRecordsService.deleteArchiveStaffPostholdersRecordsByStaffArchiveIds(staffArchiveIds);
        archiveStaffJobholderRecordsService.deleteArchiveStaffJobholderRecordsByStaffArchiveIds(staffArchiveIds);
        return toAjax(1);
    }

}
